package org.jeecg.modules.demo.yuedushenhe.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.Shenhefankuibiao.entity.Shenhefankuibiao;
import org.jeecg.modules.demo.yuedupeihe.entity.PeiheDto;
import org.jeecg.modules.demo.yuedupeihe.entity.Yuedupeihebumenbiao;
import org.jeecg.modules.demo.yuedupeihe.service.IYuedupeihebumenbiaoService;
import org.jeecg.modules.demo.yuedushenhe.entity.PeihebumenqingkuangDto;
import org.jeecg.modules.demo.yuedushenhe.entity.YueduShenheDto;
import org.jeecg.modules.demo.yuedushenhe.entity.Yuedushenhefankuibiao;
import org.jeecg.modules.demo.yuedushenhe.service.IYuedushenhefankuibiaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 审核反馈表
 * @Author: jeecg-boot
 * @Date:   2025-11-07
 * @Version: V1.0
 */
@Tag(name="审核反馈表")
@RestController
@RequestMapping("/yuedushenhe/yuedushenhefankuibiao")
@Slf4j
public class YuedushenhefankuibiaoController extends JeecgController<Yuedushenhefankuibiao, IYuedushenhefankuibiaoService> {
	@Autowired
	private IYuedushenhefankuibiaoService yuedushenhefankuibiaoService;
	@Autowired
	private IYuedupeihebumenbiaoService yuedupeihebumenbiaoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param yuedushenhefankuibiao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "审核反馈表-分页列表查询")
//	@Operation(summary="审核反馈表-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<Yuedushenhefankuibiao>> queryPageList(Yuedushenhefankuibiao yuedushenhefankuibiao,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//
//
//        QueryWrapper<Yuedushenhefankuibiao> queryWrapper = QueryGenerator.initQueryWrapper(yuedushenhefankuibiao, req.getParameterMap());
//		Page<Yuedushenhefankuibiao> page = new Page<Yuedushenhefankuibiao>(pageNo, pageSize);
//		IPage<Yuedushenhefankuibiao> pageList = yuedushenhefankuibiaoService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}
	//审核反馈列表
	 @PostMapping(value = "/list")
	 public Result<IPage<YueduShenheDto>> queryPageList(@RequestBody PeiheDto peiheDto,
													   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													   HttpServletRequest  req) {
		//获取用户id
		 LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String userId = user.getId();
		 //审核反馈表查到负责主管该记录全部配合部门的数据；
		 Page<Yuedushenhefankuibiao> page = new Page<>(pageNo, pageSize);
		 QueryWrapper<Yuedushenhefankuibiao> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("zerenzhuguan",userId);
		 queryWrapper.eq("zhuangtai","进行中");
		 queryWrapper.eq("zhidingid",peiheDto.getId());
		 IPage<Yuedushenhefankuibiao> pageList = yuedushenhefankuibiaoService.page(page, queryWrapper);
		 List<Yuedushenhefankuibiao> list = pageList.getRecords();
		 IPage<YueduShenheDto> shenhefankuibiaoDto = new Page<>();
		 List<YueduShenheDto> shenhefankuibiaoList = new ArrayList<>();
		 if(list != null && !list.isEmpty()){
			 for (Yuedushenhefankuibiao yuedushenhefankuibiao : list) {
				 YueduShenheDto shenhefankuibiao = new YueduShenheDto();
				 BeanUtils.copyProperties(yuedushenhefankuibiao,shenhefankuibiao);
				 shenhefankuibiaoList.add(shenhefankuibiao);
			 }
			 shenhefankuibiaoDto.setRecords(shenhefankuibiaoList);
		 }
		 return Result.OK(shenhefankuibiaoDto);
	 }

	 //审核反馈通过
	 @PostMapping(value = "/tongguo")
	 public Result<String> tongguo(@RequestBody Yuedushenhefankuibiao yuedushenhefankuibiao) {
		 QueryWrapper<Yuedushenhefankuibiao> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("id",yuedushenhefankuibiao.getId());
		 Yuedushenhefankuibiao ydshfkbiao = yuedushenhefankuibiaoService.getOne(queryWrapper);
		 ydshfkbiao.setZhuangtai(yuedushenhefankuibiao.getZhuangtai());
		 yuedushenhefankuibiaoService.updateById(ydshfkbiao);
		 return Result.OK("接收成功！");
	 }


	 //查看配合部门处理情况
	 @PostMapping(value = "/chakan")
	 public Result<IPage<PeihebumenqingkuangDto>> chakan(@RequestBody PeiheDto peiheDto,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														 HttpServletRequest  req){
		Page<Yuedupeihebumenbiao> page = new Page<>(pageNo, pageSize);
		QueryWrapper<Yuedupeihebumenbiao> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("zhidingid",peiheDto.getId());
		IPage<Yuedupeihebumenbiao> pageList = yuedupeihebumenbiaoService.page(page, queryWrapper);
		List<Yuedupeihebumenbiao> list = pageList.getRecords();
		IPage<PeihebumenqingkuangDto> peihebumenDto = new Page<>();
		List<PeihebumenqingkuangDto> peihebumenList = new ArrayList<>();
		if(list != null && !list.isEmpty()){
			for (Yuedupeihebumenbiao yuedupeihebumenbiao : list) {
				PeihebumenqingkuangDto peihebumen = new PeihebumenqingkuangDto();
				BeanUtils.copyProperties(yuedupeihebumenbiao,peihebumen);
				peihebumenList.add(peihebumen);
			}
			peihebumenDto.setRecords(peihebumenList);
		}
		return Result.OK(peihebumenDto);
	 }
	/**
	 *   添加
	 *
	 * @param yuedushenhefankuibiao
	 * @return
	 */
	@AutoLog(value = "审核反馈表-添加")
	@Operation(summary="审核反馈表-添加")
	@RequiresPermissions("yuedushenhe:z_shenhefankuibiao:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Yuedushenhefankuibiao yuedushenhefankuibiao) {
		yuedushenhefankuibiaoService.save(yuedushenhefankuibiao);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param yuedushenhefankuibiao
	 * @return
	 */
	@AutoLog(value = "审核反馈表-编辑")
	@Operation(summary="审核反馈表-编辑")
	@RequiresPermissions("yuedushenhe:z_shenhefankuibiao:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Yuedushenhefankuibiao yuedushenhefankuibiao) {
		yuedushenhefankuibiaoService.updateById(yuedushenhefankuibiao);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "审核反馈表-通过id删除")
	@Operation(summary="审核反馈表-通过id删除")
	@RequiresPermissions("yuedushenhe:z_shenhefankuibiao:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		yuedushenhefankuibiaoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "审核反馈表-批量删除")
	@Operation(summary="审核反馈表-批量删除")
	@RequiresPermissions("yuedushenhe:z_shenhefankuibiao:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yuedushenhefankuibiaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "审核反馈表-通过id查询")
	@Operation(summary="审核反馈表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Yuedushenhefankuibiao> queryById(@RequestParam(name="id",required=true) String id) {
		Yuedushenhefankuibiao yuedushenhefankuibiao = yuedushenhefankuibiaoService.getById(id);
		if(yuedushenhefankuibiao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yuedushenhefankuibiao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yuedushenhefankuibiao
    */
    @RequiresPermissions("yuedushenhe:z_shenhefankuibiao:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Yuedushenhefankuibiao yuedushenhefankuibiao) {
        return super.exportXls(request, yuedushenhefankuibiao, Yuedushenhefankuibiao.class, "审核反馈表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("yuedushenhe:z_shenhefankuibiao:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Yuedushenhefankuibiao.class);
    }

}
